<?php
/**
 * text
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class news_controller extends controller
{
	protected $quantity;
	
	public function on_start( )
	{
		$this ->visitor = visitor::instance( );
		$this ->model = new news( );
		$this ->quantity = _::pick( config::instance()->news_quantity, 10 );
		$this ->crumbs['/news/'] = __('News');
	}
	
	public function page_index( $p, $o )
	{
		$page = (isset($p['page'])) ? (int)$p['page'] : 1;
		
		$list = $this ->model ->by( array( ), 'date-,tm-', array( ($page-1)*$this ->quantity, $this ->quantity) );
		$found_rows = $this ->model ->get_found_rows( );
		$total_pages = ( intval( $found_rows / $this ->quantity) ) + (( $found_rows % $this ->quantity)>0 ? 1:0 );
		
		$r = array( 'list' => $list, 'total_pages' => $total_pages, 'current_page' => $page, 'title' => __('News') );
		
		return $r;
	}

	public function page_item( $p, $o )
	{
		if( !isset( $p['id'] ) ) response::__getme( ) ->notfound( );
		
		$id = (int)$p['id'];
		$item = $this ->model ->get( $id );
		
		$r = array( );
		$r['item'] = $item;
		
		$items = $this ->model ->getAbove( $id );
		$r += $items;
		
		// meta & crumbs
		$meta = array( );
		$meta['title'] = $item['title'].' — '.'Новости';
		$meta['keywords'] = '';
		$meta['short'] = texo::run( $item['short'], "plain r|\n|\s" );
		$this ->meta = $meta;
		
		$this ->crumbs['/news/'.$id.'/'] =
			$r['title'] = $item['title'];
		
		return $r;
	}

	public function block_list( $p )
	{
		$q = _::pick( @$p['quantity'], $this->quantity );
		if( !$q ) $q = 3;

		return array(
			'list' => $this->model->by( null, 'date-,tm-', $q ),
			);
	}
	
	public function page_rss( $p, $o )
	{
		$quantity = _::pick( @$p['quantity'], @$o['quantity'], $this ->quantity );
		$list = $this ->model ->by( array( ), 'date-,tm-', $quantity );
		//$v = view::instance( );
		//$v ->__Smarty ->assign( 'now', $list );
		//var_dump( $v );
		//die;
		response::instance( ) ->content_type = "application/rss+xml";
		response::instance( ) ->charset = "utf-8";
		return array( 'list' => $list, 'template' => '!rss.tpl', 'frame' => '' );
	}

	public function get_sitemap()
	{
		$i = $this->model->by( '', 'tm-', 1 );

		$r = array();
		$r[site_url('news',1)] = array(
			'href' => site_url('news'),
			'text' => __('News'),
			'tm' => $i['tm'],
			'changefreq' => 'weekly',
			'priority' => '0.6',
			);
		$r[site_url('rss',1)] = array(
			'href' => site_url('rss'),
			'text' => __('News RSS'),
			'tm' => $i['tm'],
			'changefreq' => 'weekly',
			'priority' => '0.6',
			);
		return $r;
	}
}
